﻿using DataAccess;
using DataEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebUI.Controllers
{
    public class RoleRightController : Controller
    {
        //
        // GET: /RoleRight/

        private DataContext db = new DataContext();
        /// <summary>
        /// 菜单
        /// </summary>
        private EntityRepository<SysMenu> MenuRepository;

        /// <summary>
        /// 角色权限
        /// </summary>
        private IEntityRepository<SysRoleRight> RoleRightRepository;


        /// <summary>
        /// 角色
        /// </summary>
        private IEntityRepository<SysRole> RoleRepository;

        public RoleRightController()
        {
            MenuRepository = new EntityRepository<SysMenu>(db);
            RoleRightRepository = new EntityRepository<SysRoleRight>(db);
            RoleRepository = new EntityRepository<SysRole>(db);

        }

        #region 页面

        /// <summary>
        /// 角色权限首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 为easyui tree提供角色数据 过滤掉 系统管理员 角色
        /// </summary>
        /// <returns>Json形式返回的角色数据</returns>
        [HttpPost]
        public ActionResult RoleListForTree()
        {

            var rolelist = RoleRepository.Get(n => n.BeLock == false).ToList();
            List<Object> result = new List<Object>();
            foreach (var item in rolelist)
            {
                result.Add(new { id = item.ID, text = item.Name, iconCls = "icon-role" });
            }
            return Json(result);
        }

        /// <summary>
        /// 显示用户权限
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public ActionResult List(int roleid, string rolename)
        {
            ViewBag.RoleID = roleid;
            ViewBag.RoleName = Server.HtmlDecode(rolename);

            //UserInfo = CurrentUserInfo;
            ////权限控制
            //ViewBag.Edit = Common.HasFunction(UserInfo.RangeMenuFunction, "/" + RouteData.Values["controller"].ToString() + "/Index", "修改");

            return PartialView(MenuRepository.Get(orderBy: o => o.OrderBy(m => m.SortNumber)).ToList());

        }

        #endregion

        #region 操作

        /// <summary>
        /// 保存角色权限
        /// </summary>
        /// <param name="roleFunction"></param>
        /// <param name="roleid"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Save([ModelBinder(typeof(JsonBinder<SysRoleRight>))]List<SysRoleRight> roleRight, int roleid)
        {
            try
            {


                //先删除roleid对应的所有角色权限
                IList<SysRoleRight> rf = RoleRightRepository.Get(u => u.SysRoleID == roleid).ToList();
                RoleRightRepository.Delete(rf);

                RoleRightRepository.Insert(roleRight);


                return Json(new { success = true }, "application/json");
            }
            catch (Exception e)
            {
                return Json(new { msg = e.Message });
            }

        }

        /// <summary>
        /// 清空角色权限
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Clear(int roleid)
        {
            try
            {

                if (RoleRightRepository.Get().Any(u => u.SysRoleID == roleid))
                {
                    //删除roleid对应的所有角色权限
                    IList<SysRoleRight> rf = RoleRightRepository.Get(u => u.SysRoleID == roleid).ToList();
                    RoleRightRepository.Delete(rf);

                }
                return Json(new { success = true });
            }
            catch (Exception e)
            {
                return Json(new { msg = e.Message });
            }

        }

        #endregion


    }
}
